package Demo;

import java.io.*;
import java.util.Scanner;

public class Test3 {
    public static void main(String[] args) {
//我的错误代码！！！！！
//        Writer writer=null;
//        try{
//            writer=new FileWriter("C:\\java2024\\20241127\\account.txt");
//            writer.write("zhangsan");
//            writer.write("123456");
//        }catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//            if(writer!=null){
//                try {
//                    writer.close();;
//                }catch (IOException e){
//                    e.printStackTrace();
//                }
//            }
//        }
//        Scanner scanner=new Scanner(System.in);
//        String str1=scanner.nextLine();
//        String str2=scanner.nextLine();
//        Reader reader=null;
//        try{
//            reader=new FileReader("C:\\java2024\\20241127\\account.txt");
//            char[] b=new char[1024];
//            int len;
//            while((len=reader.read(b))!=-1){
//                if(str1==new String(b,0,str1.length())&&str2==new String(b,str1.length()+1,len)){
//                    System.out.println("登录成功");
//                }else if(str1!=new String(b,0,str1.length())){
//                    System.out.println("用户名错误");
//                }else if(str2!=new String(b,str1.length()+1,len)){
//                    System.out.println("密码错误");
//                }
//            }
//        }catch (IOException e){
//            e.printStackTrace();
//        }finally {
//            if(reader!=null){
//                try {
//                    reader.close();
//                }catch (IOException e){
//                    e.printStackTrace();
//                }
//            }
//        }
        //正确代码!!!
        Writer writer=null;
        try{
            writer=new FileWriter("C:\\java2024\\20241127\\account.txt");
            writer.write("zhangsan:123456");
        }catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(writer!=null){
                try {
                    writer.close();;
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
        Scanner scanner=new Scanner(System.in);
        String str1=scanner.next();
        String str2=scanner.next();
        //String str1 = "zhangsan";
        //String str2 = "123456";
        try (Reader reader = new FileReader("C:\\java2024\\20241127\\account.txt");
             BufferedReader bufferedReader = new BufferedReader(reader)) {

            String line = bufferedReader.readLine(); // 读取文件中的一行（假设只有一行）
            if (line != null) {
                String[] parts = line.split(":"); // 使用冒号分隔用户名和密码
                if (parts.length == 2) {
                    String storedUsername = parts[0];
                    String storedPassword = parts[1];

                    if (str1.equals(storedUsername) && str2.equals(storedPassword)) {
                        System.out.println("登录成功");
                    } else if (!str1.equals(storedUsername)&&!str2.equals(storedPassword)) {
                        System.out.println("用户名和密码错误");
                    } else if(!str2.equals(storedPassword)){
                        System.out.println("密码错误");
                    }else{
                        System.out.println("用户名错误");
                    }
                } else {
                    System.out.println("文件格式错误");
                }
            } else {
                System.out.println("文件为空");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        scanner.close();
    }
}
